12 Bit DAC Audio
In this lesson the students move from their handbuild 2-bit DAC and square-wave songs to hearing a much cleaner sine wave output from a pre...
In this lesson the students move from their handbuild 2-bit DAC and square-wave songs to hearing a much cleaner sine wave output from a pre-made DAC. The 12-bit DAC from Adafruit has enough resolution to audibly demonstrate interference between signals. Below you may even see this on the plots. For example, the plots labelled freqXY means it is the plot freqX + freqY.
The students should listen for the differences between the first three notes, and then between the first three and the rest. The first three are pure sine waves, each double the frequency of the last. The next four plots are superpositions of the previous three and sound very different due to the interference.
/**************************************************************************/
/*!
@file sinewave.pde
@author Adafruit Industries
@license BSD (see license.txt)
This example will generate a sine wave with the MCP4725 DAC.
This is an example sketch for the Adafruit MCP4725 breakout board
----> http://www.adafruit.com/products/935
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
*/
/**************************************************************************/
#include <Wire.h>
#include <Adafruit_MCP4725.h>
Adafruit_MCP4725 dac;
#define DAC_RESOLUTION (6)
const PROGMEM uint16_t DACLookup_FullSine_8Bitfreq1[64] =
{750,816,881,944,1004,1062,1116,1165,1210,1250,1285,1313,1336,1352,1362,1365,1362,1352,1336,1313,1285,1250,1210,1165,1116,1062,1004,944,881,816,750,683,616,549,484,421,361,303,250,200,155,115,81,52,29,13,3,0,3,13,29,52,81,115,155,200,250,303,361,421,484,549,616,683};
const PROGMEM uint16_t DACLookup_FullSine_8Bitfreq2[64] =
{816,944,1062,1165,1250,1313,1352,1365,1352,1313,1250,1165,1062,944,816,683,549,421,303,200,115,52,13,0,13,52,115,200,303,421,549,683,816,944,1062,1165,1250,1313,1352,1365,1352,1313,1250,1165,1062,944,816,683,549,421,303,200,115,52,13,0,13,52,115,200,303,421,549,683};
const PROGMEM uint16_t DACLookup_FullSine_8Bitfreq3[64] =
{944,1165,1313,1365,1313,1165,944,683,421,200,52,0,52,200,421,683,944,1165,1313,1365,1313,1165,944,683,421,200,52,0,52,200,421,683,944,1165,1313,1365,1313,1165,944,683,421,200,52,0,52,200,421,683,944,1165,1313,1365,1313,1165,944,683,421,200,52,0,52,200,421,683};
const PROGMEM uint16_t DACLookup_FullSine_8Bitfreq12[64] =
{1566,1760,1943,2109,2254,2375,2468,2530,2562,2563,2535,2478,2398,2296,2178,2048,1911,1773,1639,1513,1400,1302,1223,1165,1129,1114,1119,1144,1184,1237,1299,1366,1432,1493,1546,1586,1611,1616,1602,1565,1507,1428,1331,1217,1091,957,819,683,552,434,332,252,196,167,168,200,263,355,476,621,787,970,1165,1366};
const PROGMEM uint16_t DACLookup_FullSine_8Bitfreq13[64] =
{1694,1981,2194,2309,2317,2227,2060,1848,1631,1450,1337,1313,1388,1552,1783,2048,2306,2517,2649,2678,2598,2415,2154,1848,1537,1262,1056,944,933,1016,1171,1366,1560,1714,1797,1786,1674,1468,1194,883,576,315,133,52,81,213,424,683,947,1178,1342,1417,1394,1280,1099,883,671,503,413,421,536,749,1037,1366};
const PROGMEM uint16_t DACLookup_FullSine_8Bitfreq23[64] =
{1760,2109,2375,2530,2563,2478,2296,2048,1773,1513,1302,1165,1114,1144,1237,1366,1493,1586,1616,1565,1428,1217,957,683,434,252,167,200,355,621,970,1366,1760,2109,2375,2530,2563,2478,2296,2048,1773,1513,1302,1165,1114,1144,1237,1366,1493,1586,1616,1565,1428,1217,957,683,434,252,167,200,355,621,970,1366};
const PROGMEM uint16_t DACLookup_FullSine_8Bitfreq123[64] =
{2510,2925,3256,3474,3567,3540,3412,3213,2983,2763,2587,2478,2450,2496,2599,2731,2855,2938,2952,2878,2713,2467,2167,1848,1550,1314,1171,1144,1236,1437,1720,2049,2376,2658,2859,2951,2924,2781,2546,2248,1928,1628,1383,1217,1143,1157,1240,1366,1496,1599,1645,1617,1509,1332,1112,883,684,555,528,621,839,1170,1586,2049};
void setup(void) {
Serial.begin(9600);
Serial.println("Hello!");
// For Adafruit MCP4725A1 the address is 0x62 (default) or 0x63 (ADDR pin tied to VCC)
// For MCP4725A0 the address is 0x60 or 0x61
// For MCP4725A2 the address is 0x64 or 0x65
dac.begin(0x62);
Serial.println("Generating a sine wave");
}
void loop(void) {
uint16_t i;
for (i = 0; i < 64; i++)
{
dac.setVoltage(pgm_read_word(&(DACLookup_FullSine_8Bitfreq13[i])), false);
}
}